package com.mkjb.utils;

import java.util.ArrayList;
import java.util.List;

public abstract class Page<T> implements Pageable<T> {

    public static int DEFAULT_PAGE_SIZE = 10;
    public static int DEFAULT_PAGE_BUTTON = 5;

    protected int currentPage = 1;          //当前页码
    protected int rowSize;        //每页行数
    protected long totalRecord;      //总记录数
    protected int totalPage;        //总页数
    protected List<T> list = new ArrayList<T>(); //分页结果

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        if (currentPage <= 0) {
            currentPage = 1;
        }
        this.currentPage = currentPage;
    }

    public int getRowSize() {
        if (rowSize > 0) {
            return rowSize;
        }
        return DEFAULT_PAGE_SIZE;
    }

    public void setRowSize(int rowSize) {
        if (rowSize <= 0) {
            rowSize = DEFAULT_PAGE_SIZE;
        }
        this.rowSize = rowSize;
    }

    public long getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(long totalCount) {
        this.totalRecord = totalCount;
        int rowSize1 = getRowSize();
        int temp = (int) (totalCount / rowSize1);
        if ((totalCount % rowSize1) > 0) {
            temp++;
        }
        totalPage = temp;
    }

    public long getStartPosition() {
        return (currentPage - 1) * getRowSize();
    }

    public abstract void addAllList(List itemList);

    public boolean isFirst() {
        return currentPage == 1;
    }

    public boolean isLast() {
        return currentPage == totalPage;
    }

    public int getBeginPageNumber() {
        int result = currentPage - (DEFAULT_PAGE_BUTTON / 2);
        return result > 0 ? result : 1;
    }

    public int getEndPageNumber() {
        int result = currentPage + (DEFAULT_PAGE_BUTTON / 2);
        return result < totalPage ? result : totalPage;
    }
}
